package libaryinterface.admin;

import libaryinterface.interfaceutils.MyDialogDemo;
import service.impl.UserServiceImpl;

import service.BookService;
import service.impl.BookServiceImpl;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.Font;
//管理员添加用户界面
public class AdminAddUserInterface extends JFrame{
    JFrame f1;
    JPanel p1;
    JComboBox c1;
    JButton b1;
    JLabel l1,l2,l3,l4,l5,l6;
    JTextField t1,t2,t3,t4,t5,t6;
    public AdminAddUserInterface()
    {
        String[] perfession = {"本科生","研究生","教师"};
        f1 = new JFrame("添加信息");
        //1.把图片添加到标签里（把标签的大小设为和图片大小相同），把标签放在分层面板的最底层；
        ImageIcon bg = new ImageIcon("src//picture//asdf.jpg");
        JLabel label = new JLabel(bg);
        label.setSize(bg.getIconWidth(),bg.getIconHeight());
        f1.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
        //2.把窗口面板设为内容面板并设为透明、流动布局。
        JPanel p1=(JPanel)f1.getContentPane();
        p1.setOpaque(false);
        p1.setLayout((new FlowLayout()));

        Font f = new Font("楷体",Font.PLAIN,20);
        b1 = new JButton("添加");
        l2 = new JLabel("账号：");
        l3 = new JLabel("密码：");
        l4 = new JLabel("昵称：");
        l5 = new JLabel("身份：");
        l6 = new JLabel("已借：");

        l2.setFont(f);
        l2.setForeground(Color.white);
        l3.setFont(f);
        l3.setForeground(Color.white);
        l4.setFont(f);
        l4.setForeground(Color.white);
        l5.setFont(f);
        l5.setForeground(Color.white);
        l6.setFont(f);
        l6.setForeground(Color.white);
//        l2.setFont(new Font("黑体", Font.PLAIN, 36));
        t2 = new JTextField(18);
        t3 = new JTextField(18);
        t4 = new JTextField(18);
        t5 = new JTextField(18);
        t6 = new JTextField(18);
        c1 = new JComboBox(perfession);
        p1.setLayout(null);
        b1.setBounds(250,450,80,30);

        l2.setBounds(160,140,80,30);
        l3.setBounds(160,200,80,30);
        l4.setBounds(160,260,80,30);
        l5.setBounds(160,320,80,30);
        l6.setBounds(160,380,80,30);

        t2.setBounds(250,140,200,30);
        t3.setBounds(250,200,200,30);
        t4.setBounds(250,260,200,30);
        c1.setBounds(250,320,200,30);
        t6.setBounds(250,380,200,30);

        p1.add(l2); p1.add(l3); p1.add(l4); p1.add(l5); p1.add(l6);
        p1.add(t2); p1.add(t3); p1.add(t4); p1.add(c1); p1.add(t6);
        p1.add(b1);

        BookService bookService = new BookServiceImpl();
        f1.setVisible(true);
        f1.setBounds(600,250,600,700);

        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                long catelogy_id = 0;
                if(c1.getSelectedItem().toString().equals("研究生"))
                {
                    catelogy_id=2;
                }
                else if(c1.getSelectedItem().toString().equals("本科生"))
                {
                    catelogy_id=1;
                }
                else if(c1.getSelectedItem().toString().equals("教师"))
                {
                    catelogy_id=3;
                }
                if(t2.getText().equals("")||t3.getText().equals("")||t4.getText().equals("")||t6.getText().equals(""))
                {
                    new MyDialogDemo("用户信息不全！");
                    return;
                }
                UserServiceImpl userService1 = new UserServiceImpl();
                boolean flag = userService1.addUser(t2.getText(), t3.getText(), t4.getText(), Long.valueOf(catelogy_id),Long.valueOf(t6.getText()));
                if( flag){
                    new MyDialogDemo("添加成功!");
                } else {
                    new MyDialogDemo("添加的账户已存在!");
                }
            }
        });


    }
}